Package vg.modules.search.components

Source Code of vg.modules.search.components.AttributeListener

package vg.modules.search.components;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

import vg.core.VisualGraph;
import vg.userInterface.swingComponents.Bracket;
import vg.userInterface.swingComponents.ValueComboBox;

public class ConditionalPanel {
  private final JPanel view;
  private JPanel conditionalPanel;
  private JScrollPane conditionalPanelScroll;
  private JButton plusButton;
  private ArrayList<JComboBox>leftParts;//left part of conditional
  private ArrayList<ValueComboBox>rightParts;//right part of conditional                                       
  private ArrayList<JButton>deleteButtons;//button for deleting of condition
  private ArrayList<JComboBox>conditionOperators;//operators between conditions(and, or)
  private ArrayList<Bracket>leftBrackets;
  private ArrayList<Bracket>rightBrackets;
  private Map<String, Set<String>>dataAttributes = null;
  /**
   * Constructor.
   */
  public ConditionalPanel() {
    //clear and initialize
    clear();
    //Package of interface-------------------
    this.view = new JPanel(new GridLayout(1, 1));
    this.conditionalPanel = new JPanel(new GridBagLayout());
    this.conditionalPanelScroll = new JScrollPane(this.conditionalPanel);
    this.view.add(this.conditionalPanelScroll);
        //build plus conditional button
        this.plusButton = new JButton("add condition");
    this.plusButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        plus();
      }
    });
  }
  /**
   * This method sets new attributes for conditions.
   * @param dataAttributes - is map, where key is name of attribute and value is array of values of this attribute.
   */
  public synchronized void setNewDataAttributes(final Map<String, Set<String>>dataAttributes) {
    this.dataAttributes = dataAttributes;
    clear();
    update();
  }
  public synchronized String getRequest(String vertexVariable, final int what) {
    StringBuffer request = new StringBuffer(4 * 1024);
    for(int i = 0; i < this.leftParts.size(); i++) {
      JComboBox nameCB = this.leftParts.get(i);
      ValueComboBox valueCB = this.rightParts.get(i);
      String name = (String)nameCB.getSelectedItem();
      String value = valueCB.getSelectedItem();
      //replace * and ? to % and _
      value.replace('*', '%');
      value.replace('?', '_');
      if(name != null && value != null) {
        if(what == 0) {
          String req = vertexVariable + " in (select v.db_id " +
                 "from vertex v, com_vertex_attribute cva, attribute a " +
                 "where a.name = '" + name + "' and a.value LIKE '%" + value + "%' and cva.db_id_vertex = v.db_id and cva.db_id_attribute = a.db_id)";
          request.append(this.leftBrackets.get(i).getText());
          request.append(req);
        } else {
          String req = vertexVariable + " in (select e.db_id " +
           "from edge e, com_edge_attribute cva, attribute a " +
           "where a.name = '" + name + "' and a.value LIKE '%" + value + "%' and cva.db_id_edge = e.db_id and cva.db_id_attribute = a.db_id)";
          request.append(this.leftBrackets.get(i).getText());
          request.append(req);         
        }
        request.append(this.rightBrackets.get(i).getText());
        if(i != this.leftParts.size() - 1) {
          request.append((String)(this.conditionOperators.get(i).getSelectedItem()));
        }
      }
    }
    return(request.toString());
  }
  /**
   * This method updates UI theme.
   */
  public synchronized void updateUITheme() {
    update();
  }
  /**
   * This method returns view of this component.
   */
  public JPanel getView() {
    return(this.view);
  }
  /**
   * This method clears conditional panel.
   */
  private synchronized void clear() {
    this.leftParts = new ArrayList<JComboBox>();
    this.rightParts = new ArrayList<ValueComboBox>();
    this.deleteButtons = new ArrayList<JButton>();
    this.conditionOperators = new ArrayList<JComboBox>();
    this.leftBrackets = new ArrayList<Bracket>();
    this.rightBrackets = new ArrayList<Bracket>();   
  }
  /**
   * Add new condition.
   */
  private synchronized void plus() {
    if(this.dataAttributes == null) {
      return;
    }
    if(this.dataAttributes.keySet() == null || this.dataAttributes.keySet().size() == 0) {
      return;
    }
    //create left and right parts of condition
    Object[] array = this.dataAttributes.keySet().toArray();
    Arrays.sort(array);
    JComboBox cbName = new JComboBox(array);
    this.leftParts.add(cbName);
    String obj = (String)cbName.getSelectedItem();//get selecting attribute
    ValueComboBox cbValue = null;
    if(obj == null) {
      VisualGraph.log.printDebug("[" + this.getClass().getName() + ".plus] selected attribute = null");
      cbValue = new ValueComboBox();
    } else {
      Set<String>value = this.dataAttributes.get(obj);
      if(value != null) {
        array = value.toArray();
        Arrays.sort(array);
        cbValue = new ValueComboBox(array);
      } else {
        cbValue = new ValueComboBox();
      }
    }
    this.rightParts.add(cbValue);
    //create delete button
    JButton delete = new JButton("Delete");
    this.deleteButtons.add(delete);
    DeleteListener dal = new DeleteListener() {
      private JComboBox cbName;
      private ValueComboBox cbValue;
      private JButton delete;
      private Bracket leftBracket, rightBracket;
      private JComboBox conditionalOperator;
      //---------------------------
      public void setParametrs(final JComboBox cbName, final ValueComboBox cbValue, final JButton delete, final Bracket leftBracket, final Bracket rightBracket, final JComboBox conditionalOperator) {
        this.cbName = cbName;
        this.cbValue = cbValue;
        this.delete = delete;
        this.leftBracket = leftBracket;
        this.rightBracket = rightBracket;
        this.conditionalOperator = conditionalOperator;
      };
      //---------------------------
      public void actionPerformed(ActionEvent e) {
        ConditionalPanel.this.leftParts.remove(this.cbName);
        ConditionalPanel.this.rightParts.remove(this.cbValue);
        ConditionalPanel.this.deleteButtons.remove(this.delete);
        ConditionalPanel.this.leftBrackets.remove(this.leftBracket);
        ConditionalPanel.this.rightBrackets.remove(this.rightBracket);
        ConditionalPanel.this.conditionOperators.remove(this.conditionalOperator);
        update();
      }
    };
    //add left and right brackets
    Bracket lb = new Bracket(Bracket.DEF_LEFT_BRACKET);
    Bracket rb = new Bracket(Bracket.DEF_RIGHT_BRACKET);
    this.leftBrackets.add(lb);
    this.rightBrackets.add(rb);
    //add or, and operator
    JComboBox co = new JComboBox();
    co.addItem("and");
    co.addItem("or");
    this.conditionOperators.add(co);
    //set parameters in delete button
    dal.setParametrs(cbName, cbValue, delete, lb, rb, co);
    delete.addActionListener(dal);
    cbName.addActionListener(new AttributeListener(cbName, cbValue, this.dataAttributes));
    //update main panel
    update();
  }
  /**
   * This method updates conditional panel
   */
  private synchronized void update() {
    //clear panel
    ConditionalPanel.this.conditionalPanel.removeAll();
    //create gbc parameter
    GridBagConstraints gbc = null;
    //add all conditions to panel
    for(int i = 0; i < ConditionalPanel.this.leftParts.size(); i++) {
      //add left bracket
      gbc = new GridBagConstraints(0,2*i,  1,11,0,  GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(3, 3, 3, 0)0,0);
      ConditionalPanel.this.conditionalPanel.add(ConditionalPanel.this.leftBrackets.get(i).getView(), gbc);
      //add left part of conditional
          gbc = new GridBagConstraints(1,2*i,  1,11,0,  GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(3, 0, 3, 0)0,0);
          ConditionalPanel.this.conditionalPanel.add(ConditionalPanel.this.leftParts.get(i), gbc);
      //add right part of conditional
      gbc = new GridBagConstraints(2,2*i,  1,11,0,  GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(3, 0, 3, 0)0,0);
      ConditionalPanel.this.conditionalPanel.add(ConditionalPanel.this.rightParts.get(i).getView(), gbc);
      //add delete button of conditional
          gbc = new GridBagConstraints(3,2*i,  1,10,0,  GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(3, 3, 3, 0)0,0);
          ConditionalPanel.this.conditionalPanel.add(ConditionalPanel.this.deleteButtons.get(i), gbc);
      //add right bracket
          gbc = new GridBagConstraints(4,2*i,  1,11,0,  GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(3, 0, 3, 3)0,0);
          ConditionalPanel.this.conditionalPanel.add(ConditionalPanel.this.rightBrackets.get(i).getView(), gbc);
          if(i != ConditionalPanel.this.leftParts.size() - 1) {
        gbc = new GridBagConstraints(0,2*i + 15,10,0,  GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(3, 3, 3, 3)0,0);
        ConditionalPanel.this.conditionalPanel.add(ConditionalPanel.this.conditionOperators.get(i), gbc);
          }
    }
    //add plus button to panel
    gbc = new GridBagConstraints(0,2*ConditionalPanel.this.leftParts.size() + 15,11,1,  GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5)0,0);
    ConditionalPanel.this.conditionalPanel.add(ConditionalPanel.this.plusButton, gbc);
    //---------------------------------------
    if(this.leftParts.size() > 0) {
      final JComboBox lastComponent = this.conditionOperators.get(this.leftParts.size() - 1);
      SwingUtilities.invokeLater(new Runnable() {
        public void run() {
          ConditionalPanel.this.view.updateUI();
          SwingUtilities.updateComponentTreeUI(lastComponent);
        }
      });
    } else {
      SwingUtilities.invokeLater(new Runnable() {
        public void run() {
          ConditionalPanel.this.view.updateUI();
        }
      });     
    }
  }
}
/**
* Interface for listener of delete button.
* @author tzolotuhin
*/
interface DeleteListener extends ActionListener {
  public void setParametrs(final JComboBox cbName, final ValueComboBox cbValue, final JButton delete, final Bracket leftBracket, final Bracket rightBracket, final JComboBox conditionalOperator);
}
/**
* This class realizes lisetner for attribute comboBox.
* @author tzolotuhin
*/
class AttributeListener implements ActionListener {
  private final JComboBox cbName;
  private final ValueComboBox cbValue;
  private final Map<String, Set<String>>data;
  public AttributeListener(JComboBox cbName, ValueComboBox cbValue, Map<String, Set<String>>data) {
    this.cbName = cbName;
    this.cbValue = cbValue;
    this.data = data;
  }
  public void actionPerformed(ActionEvent e) {
    String obj = (String)this.cbName.getSelectedItem();
   
    if(obj == null) {
      VisualGraph.log.printError("[" + this.getClass().getName() + ".actionPerformed] obj = null");
    } else {
      Set<String>value = this.data.get(obj);
      Object[] array = value.toArray();
      Arrays.sort(array);
      this.cbValue.removeAllItems();
      if(value != null) {
        for(Object buf : array) {
          this.cbValue.addItem((String)buf);
        }
      }
    }
  }
}
TOP

Related Classes of vg.modules.search.components.AttributeListener

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.